﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormRoomSizeSort : Form
    {
        public int SortID = 0;
        public FormRoomSizeSort()
        {
            InitializeComponent();
        }
        //点击保存按钮
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if(this.SortID==0)//新增
                {
                    if (CheckName())//判断名称是否已存在
                    {
                        InsertSort();
                    }
                }
                else//修改
                {
                    if (CheckName())//判断名称是否已存在
                    {
                        UpdateSort();
                    }
                }
            }
        }
        //非空验证
        private bool CheckInput()
        {
            bool flag = true;
            if (this.text_Sort.Text.Trim().Length == 0)
            {
                MessageBox.Show("名称不能为空！");
                flag = false;
                this.text_Sort.Text = "";
            }
            return flag;
        }
        //检查是否已存在
        private bool CheckName()
        {
            bool flag = true;
            DBHelper dBHelper = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("select * from RoomSize where roomsize = {0}", text_Sort.Text.Trim());
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                dBHelper.OpenConnection();
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    flag = false;
                    MessageBox.Show("名称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                reader.Close();
            }catch(Exception e)
            {
                MessageBox.Show("检查失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
            return flag;
        }
        //新增
        private void InsertSort()
        {
            DBHelper dBHelper = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("insert into RoomSize(roomsize)");
                sb.AppendFormat("values( '{0}')", text_Sort.Text.Trim());
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                dBHelper.OpenConnection();
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("添加成功！");
                    this.Close();
                }
            }catch(Exception e)
            {
                MessageBox.Show("新增失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //窗体加载
        private void FormRoomSizeSort_Load(object sender, EventArgs e)
        {
            if (this.SortID != 0)//修改
            {
                GetSortName();
                btn_Save.Text = "修改";
            }
        }
        //通过id找类型
        private void GetSortName()
        {
            DBHelper db = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("select * from RoomSize where id = {0}", SortID);
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), db.Connection);
                db.OpenConnection();
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    text_Sort.Text = reader["roomsize"].ToString();
                }
                reader.Close();
            }catch(Exception e)
            {
                MessageBox.Show("查找失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                db.CloseConnection();
            }
        }
        //修改
        private void UpdateSort()
        {
            DBHelper db = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Update RoomSize set roomsize = '{0}' where id = {1}", text_Sort.Text.Trim(),SortID);
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), db.Connection);
                db.OpenConnection();
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();
                }
            }catch(Exception e)
            {
                MessageBox.Show("修改失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                db.CloseConnection();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
